<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<style>
    .el-table .warning-row {
        background: oldlace;
    }

    .el-table .success-row {
        background: #f0f9eb;
    }
</style>
<body>
<div id="app">
    <template>
<!--        表单-->
        <el-form :inline="true" :model="brand" class="demo-form-inline">
            <el-form-item label="当前状态">
                <el-select v-model="brand.status" placeholder="当前状态">
                    <el-option label="启用" value="1"></el-option>
                    <el-option label="禁用" value="0"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="企业名称">
                <el-input v-model="brand.companyName" placeholder="企业名称"></el-input>
            </el-form-item>
            <el-form-item label="品牌名称">
                <el-input v-model="brand.brandName" placeholder="品牌名称"></el-input>
            </el-form-item>

            <el-form-item>
                <el-button type="primary" @click="onSubmit">查询</el-button>
            </el-form-item>
        </el-form>
<!--     按钮-->
        <el-row>
            <el-button type="danger" plain @click="deleteByIds">批量删除</el-button>
            <el-button type="primary" plain @click="dialogVisible = true">新增</el-button>
        </el-row>
<!--        对话框-->
        <el-dialog
                title="添加品牌"
                :visible.sync="dialogVisible"
                width="30%">
            <el-form ref="form" :model="brand" label-width="80px">
                <el-form-item label="品牌名称">
                    <el-input v-model="brand.brandName"></el-input>
                </el-form-item>
                <el-form-item label="企业名称">
                    <el-input v-model="brand.companyName"></el-input>
                </el-form-item>
                <el-form-item label="排序">
                    <el-input v-model="brand.ordered"></el-input>
                </el-form-item>
                <el-form-item label="备注">
                    <el-input type="textarea" v-model="brand.description"></el-input>
                </el-form-item>
                <el-form-item label="状态">
                    <el-switch v-model="brand.status"
                               active-value="1"
                               inactive-value="0"></el-switch>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="addBrand">提交</el-button>
                    <el-button @click="dialogVisible = false">取消</el-button>
                </el-form-item>
            </el-form>
        </el-dialog>

        <!--        表格-->
        <el-table
                :data="tableData"
                style="width: 100%"
                :row-class-name="tableRowClassName"
                @selection-change="handleSelectionChange">
            <el-table-column
                    type="selection"
                    width="55">
            </el-table-column>
            <el-table-column
                    type="index"
                    width="50">
            </el-table-column>
            <el-table-column
                    prop="brandName"
                    label="品牌名称"
                    align="center"
                  >
            </el-table-column>
            <el-table-column
                    prop="companyName"
                    label="企业名称"
                    align="center"
                    >
            </el-table-column>
            <el-table-column
                    prop="ordered"
                    align="center"
                    label="排序">
            </el-table-column>
            <el-table-column
                    prop="status"
                    align="center"
                    label="当前状态">
            </el-table-column>

            <el-table-column
                    align="center"
                    label="操作">
            <el-row slot-scope="scope">
                <el-button type="primary" @click="selectById(scope.$index, scope.row) ">修改</el-button>

                <el-button type="danger" @click="deleteById(scope.$index, scope.row)">删除</el-button>
            </el-row>

            </el-table-column>
        </el-table>
        <el-dialog
                title="修改品牌"
                :visible.sync="dialogVisible1"
                width="30%">
            <el-form ref="form" :model="brand" label-width="80px">
                <el-form-item label="品牌名称">
                    <el-input v-model="brand.brandName"></el-input>
                </el-form-item>
                <el-form-item label="企业名称">
                    <el-input v-model="brand.companyName"></el-input>
                </el-form-item>
                <el-form-item label="排序">
                    <el-input v-model="brand.ordered"></el-input>
                </el-form-item>
                <el-form-item label="备注">
                    <el-input type="textarea" v-model="brand.description"></el-input>
                </el-form-item>
                <el-form-item label="状态">
                    <el-switch v-model="brand.status"
                               active-value="1"
                               inactive-value="0"></el-switch>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="update">提交</el-button>
                    <el-button @click="dialogVisible = false">取消</el-button>
                </el-form-item>
            </el-form>
        </el-dialog>

<!--        分页-->
        <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="currentPage"
                :page-sizes="[3, 6, 8, 10]"
                :page-size="100"
                layout="total, sizes, prev, pager, next, jumper"
                :total="totalCount">
        </el-pagination>
    </template>
</div>
<script src="js/axios-0.18.0.js"></script>
<script src="js/vue.js"></script>
<script src="element-ui/lib/index.js"></script>
<link rel="stylesheet" href="element-ui/lib/theme-chalk/index.css">
<script>
    new Vue({
        el:"#app",
        mounted() {
            this.selectAll();
        },
        // 分页
        methods: {
            handleSizeChange(val) {
               // console.log(`每页 ${val} 条`);
                this.pageSize=val;
                this.selectAll();
            },
            handleCurrentChange(val) {
                // console.log(`当前页: ${val}`);
                this.currentPage=val;
                this.selectAll();
            },
            // 搜索表单
            onSubmit() {
               this.selectAll();
            },
            selectAll(){
                //查询所有
                // var _this = this;
                // axios({
                //     method: "get",
                //     url: "http://localhost:8084/test-demo/brand/selectAll"
                // }).then(function (resp) {
                //     _this.tableData = resp.data;
                // })
                //查询当前页
                // var _this = this;
                // axios({
                //     method: "post",
                //     url: "http://localhost:8084/test-demo/brand/selectByPageAndCondition?currentPage="+_this.currentPage+"&pageSize="+_this.pageSize,
                //     data:_this.brand
                // }).then(function (resp) {
                //
                //     _this.tableData = resp.data.rows;  //{rows:[],totalCount:[]}
                //     _this.totalCount=resp.data.totalCount;
                //     console.log(_this.brand)
                //
                // })

                axios({
                    method: "post",
                    url: "http://localhost:8084/test-demo/brand/selectByPageAndCondition?currentPage="+this.currentPage+"&pageSize="+this.pageSize,
                    data:this.brand
                }).then(resp =>{
                    this.tableData = resp.data.rows;  //{rows:[],totalCount:[]}
                    this.totalCount=resp.data.totalCount;

                })
            },
            addBrand(){
                var _this=this;
                axios({
                    method:"post",
                    url:"http://localhost:8084/test-demo/brand/add",
                    data:_this.brand
                }).then(function (resp) {
                    if ("success"==resp.data){
                        //如果成功，进入bran.html页面
                        _this.dialogVisible=false;
                        _this.selectAll();
                        _this.$message({
                            message:'恭喜你，添加成功！',
                            type:'success'
                        })

                    }

                })
            },
            selectById(index,row){
                console.log(row.id);
                this.updateBrandId=row.id;
                this.dialogVisible1 = true;
                var _this=this;
                axios({
                    method:"post",
                    url:"http://localhost:8084/test-demo/brand/selectById?updateBrandId="+_this.updateBrandId,
                    data:_this.brand
                }).then(function (resp) {
                    console.log( resp.data)
                    _this.brand = resp.data[0];
                })
            },
            update(){
                var _this=this;
                axios({
                    method:"post",
                    url:"http://localhost:8084/test-demo/brand/update",
                    data:_this.brand,
                }).then(function (resp) {
                    if ("success"==resp.data){
                        //如果成功，进入bran.html页面
                        _this.dialogVisible1=false;
                        _this.selectAll();
                        _this.$message({
                            message:'恭喜你，修改成功！',
                            type:'success'
                        })

                    }

                })
            },
            deleteByIds(){
                this.$confirm('此操作将删除该数据, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                  //用户确认删除
                    for(let i=0;i<this.multipleSelection.length;i++){
                        let selectionElement = this.multipleSelection[i];
                        this.selectedIds[i]=selectionElement.id;
                    }
                    var _this=this;
                    axios({
                        method:"post",
                        url:"http://localhost:8084/test-demo/brand/deleteByIds",
                        data:_this.selectedIds
                    }).then(function (resp) {
                        if ("success"==resp.data){
                            //如果成功，进入bran.html页面
                           // alert(111)
                            _this.selectAll();
                            _this.$message({
                                message:'恭喜你，删除成功！',
                                type:'success'
                            })
                        }
                    })
                    // console.log(123)
                }).catch(() => {
                  //用户取消删除
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });

            },
            deleteById(index,row){
                console.log(row.id)
                this.$confirm('此操作将删除该数据, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    //用户确认删除
                 this.deleteId=row.id;
                    var _this=this;
                    axios({
                        method:"post",
                        url:"http://localhost:8084/test-demo/brand/deleteById",
                        data:_this.deleteId
                    }).then(function (resp) {
                        if ("success"==resp.data){
                            //如果成功，进入bran.html页面
                            // alert(111)
                            _this.selectAll();
                            _this.$message({
                                message:'恭喜你，删除成功！',
                                type:'success'
                            })
                        }
                    })
                    // console.log(123)
                }).catch(() => {
                    //用户取消删除
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });


            },
            // 复选框
            handleSelectionChange(val) {

                this.multipleSelection = val;
                console.log(this.multipleSelection)
            },
            // 表格数据
            tableRowClassName({row, rowIndex}) {
                if (rowIndex === 1) {
                    return 'warning-row';
                } else if (rowIndex === 3) {
                    return 'success-row';
                }
                return '';
            }
        },
        data() {
            return {
                totalCount:100,
                pageSize:3,
                currentPage: 1,
                // currentPage4:1,
                //对话框是否显示
                dialogVisible: false,
                dialogVisible1: false,
                // 品牌表单数据
                brand: {
                    status:'',
                    companyName:'',
                    brandName:'',
                    ordered:'',
                    id:'',
                    description:''
                },
                selectedIds:[],
                // 搜索表单数据
                multipleSelection: [],
                // 表单数据
                tableData: [],
                deleteId:'',
                updateBrandId:''

            }
        }

    })
</script>

</body>
</html>